
package com.excesys.exsecs.om.utils;

import org.apache.commons.lang3.time.DateFormatUtils;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * @Title: 订单号生成
 * @author: weiyingjie
 * @date:2018年3月22日 下午7:43:30
 * @version V1.0
 */
@Service
public class OrderNoUtils {

	// 序列号的最大值
	private final int sequenceMax = 999999;

	// 序列号的最大值
	private int length = 6;

	// 这里读取的是配置文件
	// 机器id(我这里是01,正式环境建议使用机器IP)
	// 注意：分布式环境,注意每台机器的id要保证不同;也可以使用机器ip,映射成一个数字编号（如01:192.168.55.12）
	// private static String myid= SysConstant.LOCAL_MACHINE_ID;
	private static String myid = "event";

	//
	// // 示例
	private static OrderNoUtils instance = new OrderNoUtils();

	public static OrderNoUtils getInstance() {
		return instance;
	}

	/**
	 *
	 * @Title: 获取订单号
	 * @return String
	 * @Description:
	 * @author: weiyj
	 * @date: 2018年3月22日 下午7:56:56
	 */
	public static String getOrderNumber(String seqType, int sequence) {

		OrderNoUtils orderId = OrderNoUtils.getInstance();
		String nextId = orderId.nextId(seqType, sequence);

		return nextId;
	}

	/**
	 * 生成订单号
	 */
	public synchronized String nextId(String seqType, int sequence) {
		Date now = new Date();
		// String time= DateFormatUtils.format(now,"yyyyMMddHHmmssSSS");
		String time = DateFormatUtils.format(now, "yyyyMMddHHmmss");
		StringBuilder sb = new StringBuilder("");
		if (sequenceMax > sequence) {
			this.length = 6;
			sb = new StringBuilder(seqType).append(time).append(leftPad(sequence, length));
		}
		else {
			this.length = String.valueOf(sequence).length();
			sb = new StringBuilder(seqType).append(time).append(leftPad(sequence, length));
		}
		return sb.toString();
	}

	/**
	 * 生成订单名称
	 */

	public static String getOrderName(String typeName, int sequence) {

		OrderNoUtils orderId = OrderNoUtils.getInstance();
		String nextOrderName = orderId.nextOrderName(typeName, sequence);

		return nextOrderName;
	}

	public synchronized String nextOrderName(String typeName, int sequence) {
		Date now = new Date();
		// String time= DateFormatUtils.format(now,"yyyyMMddHHmmssSSS");
		String time = DateFormatUtils.format(now, "yyyyMMddHHmmss");
		StringBuilder sb = new StringBuilder("");
		if (sequenceMax > sequence) {
			this.length = 6;
			sb = new StringBuilder(typeName).append("-").append(time).append(leftPad(sequence, length));
		}
		else {
			this.length = String.valueOf(sequence).length();
			sb = new StringBuilder(typeName).append("-").append(time).append(leftPad(sequence, length));
		}
		return sb.toString();
	}

	/**
	 * 补码
	 * @param i
	 * @param n
	 * @return
	 */
	private String leftPad(long i, int n) {
		String s = String.valueOf(i);
		StringBuilder sb = new StringBuilder();
		int c = n - s.length();
		c = c < 0 ? 0 : c;
		for (int t = 0; t < c; t++) {
			sb.append("0");
		}
		return sb.append(s).toString();
	}

	/**
	 * 等待下一个毫秒的到来, 保证返回的毫秒数在参数lastTimestamp之后
	 */
	private long tilNextMillis(long lastTimestamp) {
		long timestamp = System.currentTimeMillis();
		while (timestamp <= lastTimestamp) {
			timestamp = System.currentTimeMillis();
		}
		return timestamp;
	}

}
